<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WiFi 密码配置</title>
    <style>
        .form-container {
            max-width: 500px;
            margin: 0 auto;
            padding: 2rem;
            border: 1px solid #ccc;
            border-radius: 10px;
        }
        label, input {
            display: block;
            margin-bottom: 1rem;
        }
        @media (max-width: 600px) {
            .form-container {
                padding: 1rem;
            }
        }
    </style>
</head>
<body>
    <div class="form_container">
        <h3>WiFi 密码配置</h3>
        <form>
            <label for="wifi_ssid">WiFi 名称</label>
            <input type="text" id="wifi_ssid" name="wifi_ssid" placeholder="ssid">
            <label for="wifi_password">密码</label>
            <input type="password" id="wifi_password" name="wifi_password" placeholder="password">
            <button id="send_wifi" type="button" onclick="send_wifi_data()">提交</button>
            <button id="clear_input" type="button" onclick="clear_button()">清除</button>
        </form>
    </div>
    <script>
        function send_wifi_data() {
            var wifi_ssid = document.getElementById("wifi_ssid").value;
            var wifi_password = document.getElementById("wifi_password").value;
            var xhttp = new XMLHttpRequest();
            xhttp.open("POST", "/wifi_data", true);
            xhttp.onreadystatechange = function() {
                if (xhttp.readyState == 4) {
                    if (xhttp.status == 200) {
                        console.log(xhttp.responseText);
                        alert(xhttp.responseText);
                    } else if (xhttp.status == 0) {
                        alert("Server closed the connection abruptly!");
                    } else {
                        alert(xhttp.status + " Error!\n" + xhttp.responseText);
                    }
                }
            };
            var data = {
                "wifi_ssid": wifi_ssid,
                "wifi_passwd": wifi_password
            }
            xhttp.send(JSON.stringify(data));
        }
    
        function clear_button() {
            document.getElementById("wifi_ssid").value = '';
            document.getElementById("wifi_password").value = '';
        }
    </script>
</body>
</html>
